<?php
/**
 * Show KPI reporting screen
 * 
 * 
 */

use Fox\Generator;
use Fox\Html\DropList;
use Orx\Kpi\Periodicity;
use Fox\Html\Interval;
use Fox\Html\Screen;
use Orx\Kpi;

$xview = Xview::instance();
try {
	$inlineId = $xview->val('inlineid');
	$kpi = Fox\Dao::load(Kpi::className(), $inlineId);
	if($kpi->active == 0 || !$kpi->exists()){
		Xview::instance()->accessDenied();
	}
	switch ($kpi->periodicity) {
		case Periodicity::DAY :
			$start = new DateTime();
			break;
		case Periodicity::WEEK :
			$start = Fox\Utilities\Dates::firstWeekDay(time());
			break;
		case Periodicity::MONTH :
			$start = new DateTime($today->format('Y-m').'-01');
			break;
		case Periodicity::YEAR :
			$start = new DateTime($today->format('Y').'-01-01');
			break;
	}
} catch(Fox\Object\Exception\NotExist $e) {
	Xview::instance()->accessDenied();
}

$screen = new Screen('sKpi_'.$inlineId,'execute');
$screen->pageTitle($kpi->label());

// Select Period
$screen->block(Msg(Fox\Msg::BUNDLE,Fox\Msg::EXECUTION_PARAMETERS))
		->add(new Interval('period',true,$start));
$screen->block()
		->add(new DropList(array('name','idusr')))
		->multiple()
		->options($xview->application()->users(),'idusr',Generator::call('name'));
if($screen->submitted()) {
	$fields = $screen->get();
	$data = $kpi->run($fields);
	$outputObject = $kpi->render(Kpi::RENDER_PROGRESS_BAR,$data);
}

$html = $screen->display();
if(isset($outputObject)) {
	if(is_array($outputObject)) {
		foreach($outputObject as $obj) {
			$html .= $obj->display();
		}
	} else {
		$html .= $outputObject->display();
	}
}
return $html;